package cfca.paperless.client.servlet._1_basic._1_06_CompoundSealAutoPdf;

import java.io.File;
import java.util.Date;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.ChannelBean;
import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

/**
 * @Description PDF自动化复合签章接口，使用示例。
 * 1，操作对象为本地待处理的单个PDF文件。
 * 2，使用服务端管理页面中预先制作的普通企业印章进行签章。
 * 3，签署多个普通企业印章。
 * 4，签章类型支持4种，1=空白标签签章,2=坐标签章,3=关键字签章，4=位置标识。
 * 5，返回成功签章的PDF文件。
 * 
 * @Author dell
 * @Date 2016-5-13
 * @CodeReviewer TODO
 */
public class CompoundSealAutoPdfTest02 {

    public static void main(String[] args) throws Exception {

        long timeStart = System.currentTimeMillis();// 开始时间

        System.out.println("SealAutoPdfTest01 START");
        
        PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url, 10000, 60000);
        
        

        // 如下为加盖机构印章的策略，如果没用机构印章此处可以省略
        // ************************************************************
        SignLocation signLocation = new SignLocation("银行签章", "D", 20, 20);
//        signLocation.setPage(1);
        SealUserInfo sealUserInfo = new SealUserInfo("签章者", "北京", "签合同");
        sealUserInfo.setBusinessCode("0000000");
         
        String sealCode = "yz201805266016236";
        String sealPassword = "cfca1234";
        String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, sealUserInfo, signLocation);
       
//        String sealLayer2Text = "张三";
//        String sealLayer2TextStyle = "font-size:12;";
//        String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, sealLayer2Text, sealLayer2TextStyle, sealUserInfo, signLocation)
        // ************************************************************
        

        String pdfFilePath =  "./TestData/autoCalculatePdfHash.pdf";
        
        String pdfSuffix = IoUtil.getFileNameSuffix(pdfFilePath);
        if(!PaperlessConfig.PDF_FILE_SUFFIX.equals(pdfSuffix)){
            throw new Exception("所选文件不是pdf文件");
        }
        
        byte[] pdfFile = FileUtils.readFileToByteArray(new File(pdfFilePath));
        String compoundSealStrategyXml = "<List>" + sealStrategyXml + "</List>";
        //outputFilePath = "/clsu/pdf/pdf2/" + savedPdfId + ".pdf";

        String bizTypeCode = "001";//"001"//业务类型，比如：开户、开通网银
        PdfBean pdfBean = new PdfBean(Base64.toBase64String(pdfFile), bizTypeCode);
        pdfBean.setSavedPdfId("1111111111");
        pdfBean.setPdfOwnerPassword("cfca1234");
        pdfBean.setOutputFilePath("D:/TestData/compoundSealAutopdf01.pdf");
        String pdfBeanXml = PaperlessUtil.buildPdfBeanXml(pdfBean);
        
        long start = System.currentTimeMillis();
        String channelBeanXml = new ChannelBean(PaperlessConfig.channelCode).toString();

        String systemNo = GUID.generateId();
        
        // 获取场景证书的方式，与证据证书签章策略文件配合使用。默认值为0。0：实时从CFCA CA服务申请下载场景证书；1：使用从CFCA CA服务预先申请下载并存储在本地的场景证书；
        String sceneCertChannel = "0";
        
        // 获取时间戳的方式。默认值为0。0：实时访问CFCA 时间戳服务；1：使用从CFCA购置并在本地部署的时间戳服务器产品；
        String timestampChannel = "0";
        
        byte[] sealedPdf = clientBean.compoundSealAutoPdf(systemNo,pdfBeanXml, "", compoundSealStrategyXml, PaperlessConfig.operatorCode, channelBeanXml, sceneCertChannel, timestampChannel);

        System.out.println(sealedPdf.length);
        String errorRsString = PaperlessUtil.isError(sealedPdf);
        if ("".equals(errorRsString)) {
            IoUtil.write("./TestData/output/compoundSealAutoPdf.pdf", sealedPdf);
            
        } else {
            System.out.println(new String(sealedPdf, "UTF-8"));
        }
        System.out.println("########## compoundSealAutoPdf" + " end,time:" + (System.currentTimeMillis() - start) + "##########");
    }

}
